# 常规函数
def sort_normal_func(mylist):
    for i in range(len(mylist)-1):
        for j in range(len(mylist)-i-1):
            if mylist[j]["age"] > mylist[j+1]["age"]:
                mylist[j], mylist[j+1] = mylist[j+1], mylist[j]
    return mylist


def sort_way(x):
    return x["age"]


# 使用递归来重构斐波那契函数
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)


if __name__ == "__main__":
    classes = [
        {"name": "abyss", "age": 25, "grade": "A"},
        {"name": "kin", "age": 23, "grade": "B"},
        {"name": "tian", "age": 24, "grade": "B"},
        {"name": "hun", "age": 26, "grade": "A"},
        {"name": "exe", "age": 21, "grade": "A"}
    ]

    # 通过匿名函数和常规函数根据键age来排序
    print(f"常规函数：{str(sort_normal_func(classes))}")
    classes.sort(key=sort_way)
    print(f"内置函数：\n{classes}")
    classes.sort(key=lambda x: x["age"])
    print(f"匿名函数：\n{classes}")

    # 2021年马上就要到来了, 通过map函数将上述同学的age+1
    _m = map(lambda x: x["age"] + 1, classes)
    ages = list(_m)
    for class_num in range(len(classes)):
        classes[class_num]["age"] = ages[class_num]
    print(f"age+1之后：\n{classes}")

    # 通过filter函数过滤掉上述同学中grade是A的同学
    f = filter(lambda x: x["grade"] != "A", classes)
    print(f"过滤grade是A的同学之后:\n{list(f)}")

    # 使用递归来重构斐波那契函数
    try:
        print(f"通过构造递归斐波那契函数：{fibonacci(4)}")
        print(f"斐波那契函数{fibonacci('ss')}")
    except TypeError:
        print("请输入整型")





